package com.atguigu.interview.study.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

class MyThread implements Callable<Integer>
{

    @Override
    public Integer call() throws Exception {
        System.out.println("*********come in Callable ");
        // 暂停一会线程
        try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
        return 1024;
    }
}

/**
 * @author gcq
 * @Create 2020-11-27
 */
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask = new FutureTask<>(new MyThread());


        Thread thread = new Thread(futureTask);
        thread.start();

        int result01 = 100;

       /* while (!futureTask.isDone()) {

        }*/

        int result02 = futureTask.get(); // 要求获得Callable线程的计算结果，如果没有计算完成就强求，会导致线程阻塞，等到计算完成

        System.out.println(Thread.currentThread().getName()+"\t" + (result01 + result02));
    }
}